<template>
  <el-dialog
    title="修改密码"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="15">
          <el-form-item label="账号:">
            <span>{{ dataForm.username }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="昵称:">
            <span>{{ dataForm.nickname }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="密码:" prop="password">
            <el-input type="password" v-model="dataForm.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="确认密码:" prop="confirmPassword">
            <el-input type="password" v-model="dataForm.confirmPassword"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (this.dataForm.password === "") {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.password !== value) {
          callback(new Error('确认密码与新密码不一致'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          userId: 0,
          username: '',
          nickname: '',
          password: '',
          confirmPassword: ''
        },
        dataRule: {
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateConfirmPassword, trigger: 'blur' }
          ],
        }
      }
    },
    computed: {

    },
    methods: {
      // 初始化
      init (val) {
        this.visible = true
        this.dataForm.userId = val.userId
        this.dataForm.username = val.username
        this.dataForm.nickname = val.nickname
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/theme/designer/password'),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId,
                'password': this.dataForm.password
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

